import { errorLogMock } from './data/error'
import { getNowDateTime, isDateTimeInRange } from '../../utils'
let errorLogList = errorLogMock(400)
export default [
  {
    url: '/infra/api-error-log/update-status', // 更新状态
    method: 'put',
    response: data => {
      updateStatusById(data)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: RegExp('/infra/api-error-log/page' + '.*'), // 查询分页列表
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListByPage(params)
      }
    }
  },
  {
    url: RegExp('/infra/api-error-log/export' + '.*'), // 导出
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListByExport(params)
      }
    }
  }
]

const updateStatusById = data => {
  errorLogList.forEach(item => {
    if (item.id === data.body.id) {
      Object.keys(data.body).forEach(v => {
        item[v] = data.body[v]
      })
      item.updater = '1'
      item.update_time = getNowDateTime()
    }
  })
}

const getListByPage = params => {
  let result = errorLogList
  result = result.filter(item => !item.deleted)
  if (params.query.userId) {
    result = result.filter(item => item.userId === parseInt(params.query.userId))
  }
  if (params.query.userType) {
    result = result.filter(item => item.userType === parseInt(params.query.userType))
  }
  if (params.query.applicationName) {
    result = result.filter(item => item.applicationName.indexOf(params.query.applicationName) !== -1)
  }
  if (params.query.processStatus) {
    result = result.filter(item => item.processStatus === parseInt(params.query.processStatus))
  }
  if (params.query.exceptionTime && params.query.exceptionTime.length !== 0) {
    result = result.filter(item => isDateTimeInRange(params.query.exceptionTime[0], params.query.exceptionTime[1], item.exceptionTime))
  }
  return {
    list: result.slice(parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1), parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1) + parseInt(params.query.pageSize)),
    total: result.length
  }
}

const getListByExport = params => {
  let result = errorLogList
  result = result.filter(item => !item.deleted)
  if (params.query.userId) {
    result = result.filter(item => item.userId === parseInt(params.query.userId))
  }
  if (params.query.userType) {
    result = result.filter(item => item.userType === parseInt(params.query.userType))
  }
  if (params.query.applicationName) {
    result = result.filter(item => item.applicationName.indexOf(params.query.applicationName) !== -1)
  }
  if (params.query.processStatus) {
    result = result.filter(item => item.processStatus === parseInt(params.query.processStatus))
  }
  if (params.query.exceptionTime && params.query.exceptionTime.length !== 0) {
    result = result.filter(item => isDateTimeInRange(params.query.exceptionTime[0], params.query.exceptionTime[1], item.exceptionTime))
  }
  return result
}
